module controller.MovieVlinkController;

import std.json : JSONValue, parseJSON;
import std.conv : to;

import archttp.HttpStatusCode : HttpStatusCode;
import archttp.HttpRequest : HttpRequest;
import archttp.HttpResponse : HttpResponse;

import model.MovieVlink;
import repository.MovieLinksRepo;
import utils.ObjMapping : ObjMapping;
import utils.RespData : RespData;
import utils.SecurityVerification : signValidator;

// 电影视频链接控制器
class MovieVlinkController
{

    MovieLinksRepo _mlrepo;
    this ()
    {
        _mlrepo = new MovieLinksRepo();
    }

    // 获取一个
    public void getOne (HttpRequest req, HttpResponse resp)
    {
        string idStr = req.param("id");
        if (idStr == "")
        {
            resp.json(RespData.newData(400, "错误的请求")).send();
        }
        uint id = to!uint(idStr);
        MovieVlink mlink = _mlrepo.getOneMovieLink(id);
        if (mlink is null)
        {
            resp.code(HttpStatusCode.NOT_FOUND).json(RespData.newData(404, "未找到")).send();
            return;
        }

        JSONValue respData = RespData.newData!(MovieVlinkDto)(200, "ok", ObjMapping.mappingToObject!(MovieVlinkDto, MovieVlink)(
                mlink));
        resp.json(respData).send();
    }

    // 添加链接
    public void addLink (HttpRequest req, HttpResponse resp)
    {
        JSONValue json = parseJSON(req.body());
        string movie_id = json["movie_id"].str;
        string name = json["name"].str;
        string url = json["url"].str;
        string real_url = json["real_url"].str;

        string sign = json["sign"].str;

        import std.format : format;

        string reqParamsStr = format("%s%s", name, url);
        if (!signValidator(reqParamsStr, sign))
        {
            resp.code(HttpStatusCode.FORBIDDEN).json(RespData.newData(403, "签名不正确！")).send();
            return;
        }

        MovieVlinkDtoFromCreate ml = new MovieVlinkDtoFromCreate(movie_id, name, url, real_url);

        bool res = _mlrepo.addMovieLink(ObjMapping.mappingToObject!(MovieVlink, MovieVlinkDtoFromCreate)(
                ml));
        if (!res)
        {
            resp.code(HttpStatusCode.INTERNAL_SERVER_ERROR)
                .json(RespData.newData(500, "error")).send();
            return;
        }
        resp.code(HttpStatusCode.CREATED).json(RespData.newData(200, "ok")).send();
    }

    // 删除一个链接
    public void removeLink (HttpRequest req, HttpResponse resp)
    {
        string idStr = req.param("id");
        string sign = req.query("sign");
        if (idStr == "" || sign == "" || sign is null)
        {
            resp.json(RespData.newData(400, "错误的请求")).send();
        }
        uint id = to!uint(idStr);

        if (!signValidator(idStr, sign))
        {
            resp.code(HttpStatusCode.FORBIDDEN).json(RespData.newData(403, "签名不正确！")).send();
            return;
        }

        bool res = _mlrepo.removeMovieLink(id);
        if (!res)
        {
            resp.code(HttpStatusCode.INTERNAL_SERVER_ERROR)
                .json(RespData.newData(500, "error")).send();
            return;
        }
        resp.json(RespData.newData(200, "ok")).send();
    }

    // 编辑链接
    public void editLink (HttpRequest req, HttpResponse resp)
    {
        string idStr = req.param("id");
        if (idStr == "")
        {
            resp.json(RespData.newData(400, "错误的请求")).send();
        }
        uint id = to!uint(idStr);

        JSONValue json = parseJSON(req.body());
        string name = json["name"].str;
        string url = json["url"].str;
        string real_url = json["real_url"].str;

        string sign = json["sign"].str;

        import std.format : format;

        string reqParamsStr = format("%s%s", name, url);
        if (!signValidator(reqParamsStr, sign))
        {
            resp.code(HttpStatusCode.FORBIDDEN).json(RespData.newData(403, "签名不正确！")).send();
            return;
        }

        MovieVlinkDtoFromUpdate ml = new MovieVlinkDtoFromUpdate(name, url, real_url);

        bool res = _mlrepo.editMovieLink(id, ObjMapping.mappingToObject!(MovieVlink, MovieVlinkDtoFromUpdate)(
                ml));
        if (!res)
        {
            resp.code(HttpStatusCode.INTERNAL_SERVER_ERROR)
                .json(RespData.newData(500, "error")).send();
            return;
        }
        resp.json(RespData.newData(200, "ok")).send();
    }
}
